#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->encoder=new Encoder(QCoreApplication::applicationDirPath()+"/qrencode/",this);

    QObject::connect(encoder, SIGNAL(imageLoaded(QImage)),
                     this, SLOT(loadQrImage(QImage)));
    this->ui->saveButton->setEnabled(false);




}

void MainWindow::loadQrImage(QImage img){
    if(img.isNull()) return;
    this->ui->saveButton->setEnabled(true);
    this->img = img;
    this->ui->label->setPixmap(QPixmap::fromImage(img));
    this->ui->label->resize(img.size());
}

MainWindow::~MainWindow()
{
    delete ui;
    delete encoder;
}

void MainWindow::createQrCode(){
    this->ui->label->clear();
    QHash<QString,QString> arguments;
    arguments["data"] = this->ui->textEdit->toPlainText();
    arguments["-s"] = "5";


    this->encoder->encode(arguments);
    //this->setCentralWidget(this->ui->label);

}

void MainWindow::saveImage(){
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
                               "output.png",
                               tr("Images (*.png *.xpm *.jpg)"));
    this->img.save(fileName);
}
